package io.github.echarts.model.theme;

import io.github.echarts.option.style.LineStyle;
import io.github.echarts.option.style.TextStyle;

import java.util.HashMap;
import java.util.Map;

/**
 * 默认主题
 * <p>
 * 提供ECharts默认视觉风格的主题实现。
 * 包含默认配色方案、文本样式、线条样式等。
 * </p>
 */
public class DefaultTheme implements Theme {
    
    /**
     * 主题名称
     */
    private static final String NAME = "default";
    
    /**
     * 配色方案
     */
    private final ColorScheme colorScheme;
    
    /**
     * 组件主题配置映射表
     */
    private final Map<String, Object> componentThemes = new HashMap<>();
    
    /**
     * 构造函数
     */
    public DefaultTheme() {
        this.colorScheme = new DefaultColorScheme();
        initComponentThemes();
    }
    
    /**
     * 初始化组件主题配置
     */
    private void initComponentThemes() {
        // 初始化标题主题
        initTitleTheme();
        
        // 初始化轴主题
        initAxisTheme();
        
        // 初始化图例主题
        initLegendTheme();
        
        // 初始化网格主题
        initGridTheme();
        
        // 初始化提示框主题
        initTooltipTheme();
        
        // 初始化工具箱主题
        initToolboxTheme();
    }
    
    /**
     * 初始化标题主题
     */
    private void initTitleTheme() {
        Map<String, Object> titleTheme = new HashMap<>();
        
        TextStyle textStyle = new TextStyle();
        textStyle.setColor(getColorScheme().getTextColor());
        textStyle.setFontFamily("sans-serif");
        textStyle.setFontSize(18);
        textStyle.setFontWeight("bold");
        
        TextStyle subtextStyle = new TextStyle();
        subtextStyle.setColor("#999999");
        subtextStyle.setFontFamily("sans-serif");
        subtextStyle.setFontSize(14);
        
        titleTheme.put("textStyle", textStyle);
        titleTheme.put("subtextStyle", subtextStyle);
        titleTheme.put("left", "center");
        titleTheme.put("top", 10);
        
        componentThemes.put("title", titleTheme);
    }
    
    /**
     * 初始化轴主题
     */
    private void initAxisTheme() {
        Map<String, Object> axisTheme = new HashMap<>();
        
        // 轴线样式
        LineStyle axisLineStyle = new LineStyle();
        axisLineStyle.setColor(getColorScheme().getAxisLineColor());
        axisLineStyle.setWidth(1);
        axisTheme.put("axisLineStyle", axisLineStyle);
        
        // 轴标签样式
        TextStyle axisLabelStyle = new TextStyle();
        axisLabelStyle.setColor(getColorScheme().getTextColor());
        axisLabelStyle.setFontSize(12);
        axisTheme.put("axisLabelStyle", axisLabelStyle);
        
        // 分隔线样式
        LineStyle splitLineStyle = new LineStyle();
        splitLineStyle.setColor(getColorScheme().getSplitLineColor());
        splitLineStyle.setType("dashed");
        splitLineStyle.setWidth(1);
        axisTheme.put("splitLineStyle", splitLineStyle);
        
        // 刻度样式
        LineStyle tickLineStyle = new LineStyle();
        tickLineStyle.setColor(getColorScheme().getAxisLineColor());
        tickLineStyle.setWidth(1);
        axisTheme.put("tickLineStyle", tickLineStyle);
        
        componentThemes.put("axis", axisTheme);
    }
    
    /**
     * 初始化图例主题
     */
    private void initLegendTheme() {
        Map<String, Object> legendTheme = new HashMap<>();
        
        TextStyle textStyle = new TextStyle();
        textStyle.setColor(getColorScheme().getTextColor());
        textStyle.setFontSize(12);
        
        legendTheme.put("textStyle", textStyle);
        legendTheme.put("itemGap", 10);
        legendTheme.put("inactiveColor", "#ccc");
        
        componentThemes.put("legend", legendTheme);
    }
    
    /**
     * 初始化网格主题
     */
    private void initGridTheme() {
        Map<String, Object> gridTheme = new HashMap<>();
        
        gridTheme.put("borderColor", getColorScheme().getAxisLineColor());
        gridTheme.put("borderWidth", 1);
        gridTheme.put("containLabel", true);
        
        componentThemes.put("grid", gridTheme);
    }
    
    /**
     * 初始化提示框主题
     */
    private void initTooltipTheme() {
        Map<String, Object> tooltipTheme = new HashMap<>();
        
        tooltipTheme.put("backgroundColor", "rgba(50, 50, 50, 0.7)");
        tooltipTheme.put("borderColor", "#333");
        tooltipTheme.put("borderWidth", 0);
        tooltipTheme.put("textStyle", new TextStyle().setColor("#fff").setFontSize(14));
        tooltipTheme.put("padding", 5);
        
        componentThemes.put("tooltip", tooltipTheme);
    }
    
    /**
     * 初始化工具箱主题
     */
    private void initToolboxTheme() {
        Map<String, Object> toolboxTheme = new HashMap<>();
        
        toolboxTheme.put("iconStyle", new HashMap<String, Object>(){{
            put("normal", new HashMap<String, Object>(){{
                put("borderColor", "#999");
                put("color", "#999");
            }});
            put("emphasis", new HashMap<String, Object>(){{
                put("borderColor", "#666");
                put("color", "#666");
            }});
        }});
        
        componentThemes.put("toolbox", toolboxTheme);
    }
    
    @Override
    public String getName() {
        return NAME;
    }
    
    @Override
    public ColorScheme getColorScheme() {
        return colorScheme;
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public <T> T getComponentTheme(String componentType) {
        return (T) componentThemes.get(componentType);
    }
    
    @Override
    public TextStyle getTextStyle() {
        TextStyle style = new TextStyle();
        style.setColor(getColorScheme().getTextColor());
        style.setFontFamily("sans-serif");
        style.setFontSize(12);
        return style;
    }
    
    @Override
    public LineStyle getLineStyle() {
        LineStyle style = new LineStyle();
        style.setColor(getColorScheme().getAxisLineColor());
        style.setWidth(1);
        style.setType("solid");
        return style;
    }
    
    @Override
    public String getBackgroundColor() {
        return getColorScheme().getBackgroundColor();
    }
    
    @Override
    public String getAxisLineColor() {
        return getColorScheme().getAxisLineColor();
    }
    
    @Override
    public String getSplitLineColor() {
        return getColorScheme().getSplitLineColor();
    }
    
    @Override
    public String getSeriesColor(int index) {
        return getColorScheme().getColor(index);
    }
    
    /**
     * 默认配色方案
     */
    private static class DefaultColorScheme implements ColorScheme {
        
        /**
         * 默认系列颜色
         */
        private static final String[] DEFAULT_COLORS = {
            "#5470c6", "#91cc75", "#fac858", "#ee6666",
            "#73c0de", "#3ba272", "#fc8452", "#9a60b4", "#ea7ccc"
        };
        
        @Override
        public String getName() {
            return "default";
        }
        
        @Override
        public java.util.List<String> getColors() {
            return java.util.Arrays.asList(DEFAULT_COLORS);
        }
        
        @Override
        public String getBackgroundColor() {
            return "#ffffff";
        }
        
        @Override
        public String getTextColor() {
            return "#333333";
        }
        
        @Override
        public String getAxisLineColor() {
            return "#cccccc";
        }
        
        @Override
        public String getSplitLineColor() {
            return "#eeeeee";
        }
        
        @Override
        public String getEmphasisColor() {
            return "#5470c6";
        }
        
        @Override
        public String getWarningColor() {
            return "#fac858";
        }
        
        @Override
        public String getSuccessColor() {
            return "#91cc75";
        }
        
        @Override
        public String getErrorColor() {
            return "#ee6666";
        }
    }
} 